<?php

namespace app\admin\model\prl;

use think\Model;
use fast\Tree;

class Channels extends Model
{

    

    // 表名
    protected $table = 'prl_channels';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'status_text',
        'display_text',
        'jump_child_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getTypeList()
    {
        return ['0' => __('Type 0'), '1' => __('Type 1'), '2' => __('Type 2'), '4' => __('Type 4'), '5' => __('Type 5'), '6' => __('Type 6')];
    }     

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }     

    public function getDisplayList()
    {
        return ['0' => __('Display 0'), '1' => __('Display 1')];
    }     

    public function getJumpChildList()
    {
        return ['0' => __('Jump_child 0'), '1' => __('Jump_child 1')];
    }     


    public function getTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDisplayTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['display']) ? $data['display'] : '');
        $list = $this->getDisplayList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getJumpChildTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['jump_child']) ? $data['jump_child'] : '');
        $list = $this->getJumpChildList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function views($field = 0)
    {
        $typeList = ['home_view_id', 'channel_view_id', 'list_view_id', 'info_view_id', 'page_view_id', 'form_view_id'];
        return $this->hasOne('Views', 'id', $typeList[$field]);
    }

    public function viewsFieldsValue()
    {
        return $this->hasMany('Channelsviewvalues');
    }

    public function getTreeArray()
    {
        $tree = Tree::instance();
        $list = collection($this->order('weigh asc,id desc')
            ->select())->toArray();
        $tree->init($list, 'pid');
        return $tree->getTreeList($tree->getTreeArray(0), 'name');
    }

    public function articles()
    {
        return $this->hasMany('Articles');
    }
}
